package com.github.prontera.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.domain.ClientIssueAnswer;
import com.github.prontera.domain.Issue;
import com.github.prontera.domain.IssueAnswer;
import com.github.prontera.persistence.ClientIssueAnswerMapper;
import com.github.prontera.persistence.CrudMapper;
import com.github.prontera.persistence.IssueAnswerMapper;
import com.github.prontera.util.Page;

/**
 * 问答
 */
@Service
public class IssueAnswerService extends CrudServiceImpl<IssueAnswer> {

	@Autowired
	private IssueAnswerMapper mapper;
	@Autowired
	private ClientIssueAnswerMapper clientIssueAnswerMapper;

	@Autowired
	public IssueAnswerService(CrudMapper<IssueAnswer> mapper) {
		super(mapper);
	}

	public boolean updateViewCount(Long id) {
		return mapper.updateViewCount(id) > 0;
	}
	
	public boolean updateThumbsUpCount(IssueAnswer issueAnswer) {
		if(mapper.updateThumbsUpCount(issueAnswer) <=0){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "参数错误");
		}
		if(issueAnswer.getHasThumbsUp()){
			ClientIssueAnswer clientIssueAnswer=new ClientIssueAnswer();
			clientIssueAnswer.setClientId(issueAnswer.getClientId());
			clientIssueAnswer.setClientIssueId(issueAnswer.getId());
			return clientIssueAnswerMapper.insert(clientIssueAnswer)>0;
		}else{
			ClientIssueAnswer clientIssueAnswer=new ClientIssueAnswer();
			clientIssueAnswer.setClientId(issueAnswer.getClientId());
			clientIssueAnswer.setClientIssueId(issueAnswer.getId());
			return clientIssueAnswerMapper.remove(clientIssueAnswer)>0;
		}
	}

	public IssueAnswer getOne(Long id) {
		return mapper.getOne(id);
	}

	public int updateShareCount(Long id) {
		return mapper.updateShareCount(id);
	}

	public List<IssueAnswer> findMyIssue(Issue issue) {
		return mapper.findMyIssue(issue);
	}

	public Page<IssueAnswer> page2(IssueAnswer entity) {
		long count = mapper.count(entity);
		if(entity.getPageSize()==null||entity.getPageSize()<2||entity.getPageSize()>100){
			 entity.setPageSize(10);
		}
		if(entity.getPageIndex()==null){
			 entity.setPageIndex(1);
		}
		Page<IssueAnswer> page = new Page<IssueAnswer>(entity.getPageIndex(), entity.getPageSize());
		page.setData(mapper.findManage(entity));
		page.setTotalCount(count);
		return page;
	}

	public IssueAnswer findByClientId(Long clientId) {
		return mapper.findByClientId(clientId);
	}

}
